package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "购物车")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    // 添加购物车
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        //1.接受请求参数
        log.info("添加购物车");
        //2. 调用service
        shoppingCartService.addCart(shoppingCartDTO);

        return Result.success();

    }

    // 查看购物车
    @GetMapping("/list")
    public Result listCart() {
        log.info("查看购物车");
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartService.listCart(userId);
        return Result.success(shoppingCartList);
    }

    // 清空购物车
    @DeleteMapping("/clean")
    public Result deleteCart(){
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.deleteCart(userId);
        return Result.success();
    }

    //删除购物车中一个商品
    @PostMapping("/sub")
    public Result subCart(@RequestBody ShoppingCartDTO shoppingCartDTO ){
        shoppingCartService.subCart(shoppingCartDTO);
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts = shoppingCartService.listCart(userId);
        return Result.success(shoppingCarts);
    }
}
